<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\1\6 0006
 * Time: 10:44
 */
namespace app\api\controller\v1;

use app\common\lib\exception\ApiException;
use think\Cache;
use think\Log;

class Recharge extends Common {
    /**
     * 生成学费充值待支付订单
     * @param int user_id 用户id *必传
     * @param float tuition 充值的学费数额 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function tuitionRecharge(){
        $data = $_POST;

        if (empty($data['user_id']) && !isset($data['user_id'])) {
            throw new ApiException('user_id(用户id) is not found',404);
        }

        if (empty($data['tuition']) && !isset($data['tuition'])) {
            throw new ApiException('tuition(充值费用) is not found',404);
        }

        try{
            //生成流水号
            $data['account_sn'] = date('YmdHis').rand(1000,9999);
            $data['cost_type'] = 2;
            $data['account_type'] = 1;
            $data['cost_description'] = '学费充值';

            //设置充值订单待支付时间为10分钟
            Cache::set('account:'.$data['account_sn'],$data,10*60);

            //创建支付订单
            $return = [
                'body' => '学费充值',
                'subject' => '学费充值',
                'out_trade_no' => $data['account_sn'],
                'total_amount' => $data['tuition'],
            ];

            return show(config('code.success'),'创建订单成功！',$return);
        }catch(\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }

    /**
     * 生成学币充值待支付订单
     * @param int user_id 用户id *必传
     * @param float tuition 充值的学币数额 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function coinRecharge(){
        $data = $_POST;

        if (empty($data['user_id']) && !isset($data['user_id'])) {
            throw new ApiException('user_id(用户id) is not found',404);
        }

        if (empty($data['coin']) && !isset($data['coin'])) {
            throw new ApiException('coin(充值费用) is not found',404);
        }

        try{
            //生成流水号
            $data['account_sn'] = date('YmdHis').rand(1000,9999);
            $data['cost_type'] = 1;
            $data['account_type'] = 1;
            $data['cost_description'] = '学币充值';
            //设置充值订单待支付时间为10分钟
            Cache::set('account:'.$data['account_sn'],$data,10*60);

            //创建支付订单
            $return = [
                'body' => '学币充值',
                'subject' => '学币充值',
                'out_trade_no' => $data['account_sn'],
                'total_amount' => $data['coin'],
            ];

            return show(config('code.success'),'创建订单成功！',$return);
        }catch(\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }
}